package cn.com.elite;

import cn.com.service.GeneralService;
import groovy.lang.GroovyClassLoader;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @projectName: ngsapitask-sw
 * @package: cn.com.elite
 * @className: ThreadTaskBean
 * @author: LiJT
 * @description: TODO
 * @date: 2022-10-29 02:11
 * @version: 1.0
 */

public class ThreadTaskBean {
    private static final Logger log = LoggerFactory.getLogger(ThreadTaskBean.class);
    private ThreadPoolTaskExecutor poolTaskExecutor;
    private EliteBean eliteBean;
    private String dataSourceID;
    private String sql;
    private String groovyPath;
    private Class groovyClass = null;
    private static GroovyClassLoader loader = Util.getGroovyClassLoader();

    public ThreadTaskBean() {
    }

    public ThreadPoolTaskExecutor getPoolTaskExecutor() {
        return this.poolTaskExecutor;
    }

    public void setPoolTaskExecutor(ThreadPoolTaskExecutor poolTaskExecutor) {
        this.poolTaskExecutor = poolTaskExecutor;
    }

    public EliteBean getEliteBean() {
        return this.eliteBean;
    }

    public void setEliteBean(EliteBean eliteBean) {
        this.eliteBean = eliteBean;
    }

    public String getDataSourceID() {
        return this.dataSourceID;
    }

    public void setDataSourceID(String dataSourceID) {
        this.dataSourceID = dataSourceID;
    }

    public String getSql() {
        return this.sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public String getGroovyPath() {
        return this.groovyPath;
    }

    public void setGroovyPath(String groovyPath) {
        this.groovyPath = groovyPath;
    }

    public void doTask() throws Exception {
        log.debug("多线程脚本启动GroovyPath:" + this.groovyPath);
        if (this.groovyClass == null) {
            this.groovyClass = loader.parseClass(new File(GeneralService.groovyPath + File.separator + this.groovyPath));
        }

        List list = this.eliteBean.chaxun_sql(this.sql, this.dataSourceID);
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); ++i) {
                try {
                    Map map = (Map) list.get(i);
                    String thread_guid = (String) map.get("thread_guid");
                    String status = (String) map.get("status");
                    String content = (String) map.get("content");
                    log.debug(this.groovyPath + " 线程Guid:" + thread_guid + " Status:" + status + " 线程配置信息:" + content + "...");
                    if ("0".equals(status)) {
                        log.debug(this.groovyPath + " 线程Guid:" + thread_guid + " 正常状态-运行...");
                        this.doTaskDetail(thread_guid, content);
                    } else if ("-1".equals(status)) {
                        log.debug(this.groovyPath + " 线程Guid:" + thread_guid + " 执行失败-重试...");
                        this.doTaskDetail(thread_guid, content);
                    }
                } catch (Exception var7) {
                    log.error(Util.getERRO(var7));
                }
            }
        }

    }

    private boolean doTaskDetail(String thread_guid, String content) {
        boolean retrunResult = false;

        try {
            int count = this.poolTaskExecutor.getActiveCount();
            int maxcount = this.poolTaskExecutor.getMaxPoolSize();
            log.debug("[" + thread_guid + "]当前线程池活动线程总数: " + count + " 配置最大运行线程数量:" + maxcount);
            if (count < maxcount) {
                EliteThread eliteThread = new EliteThread(this.groovyClass, this.eliteBean, thread_guid, content, this.dataSourceID);
                this.poolTaskExecutor.execute(eliteThread);
            } else {
                log.debug("[" + thread_guid + "]当前线程池活动线程已达到配置最大运行线程数量");

            }
        } catch (Exception var6) {
            log.error(Util.getERRO(var6));
        }

        return retrunResult;
    }
}